<template>
  <div class="mod-config">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="box-card" shadow="never">
          <el-tree :props="defaultProps" :data="treelist" :expand-on-click-node="false" :filter-node-method="filterNode"
            @node-click="clickNodeTreeData" class="filter-tree" highlight-current node-key="id" ref="leftDictTree">

            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span>{{ data.name }}</span>
              <span style="text-align: right;">
                <el-button type="text" size="mini" @click="() => remove(node, data)">
                  删除
                </el-button>
              </span>
            </span>
          </el-tree>
        </el-card>
      </el-col>
      <el-col :span="18">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button v-if="isAuth('sellerapp:appsysmenu:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
            <el-button v-if="isAuth('sellerapp:appsysmenu:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
<!--            <el-button v-if="isAuth('sellerapp:appsysmenu:save')" type="primary" @click="viewApi()">权限接口</el-button>
            <el-button v-if="isAuth('sellerapp:appsysmenu:save')" type="primary" @click="viewBaoBiaoApi()">报表接口</el-button> -->
          </el-form-item>
        </el-form>
        <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
          <el-table-column type="selection" header-align="center" align="center" width="50">
          </el-table-column>
          <el-table-column prop="parentId" header-align="center" align="center" label="上级">
            <template slot-scope="scope">
              <span v-if="scope.row.parentId == 0">顶级</span>
              <span v-else>{{scope.row.parentName}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="type" header-align="center" align="center" label="类型" width="80">
            <template slot-scope="scope">
              <span v-if="scope.row.type == 1">目录</span>
              <span v-if="scope.row.type == 2">菜单</span>
              <span v-if="scope.row.type == 3">按钮</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" header-align="center" align="center" label="名称">
            <template slot-scope="scope">
              <el-input size="mini" v-model="scope.row.name" @change="handleOrderNumChange(scope.row)" label="名称"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="pcName" header-align="center" align="center" label="PC标识">
            <template slot-scope="scope">
              <el-input size="mini" v-model="scope.row.pcName" @change="handleOrderNumChange(scope.row)" label="PC标识"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="reqUri" header-align="center" align="center" label="请求">
            <template slot-scope="scope">
              <el-input size="mini" v-model="scope.row.reqUri" @change="handleOrderNumChange(scope.row)" label="请求"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="permissions" header-align="center" align="center" label="权限">
            <template slot-scope="scope">
              <el-input size="mini" v-model="scope.row.permissions" @change="handleOrderNumChange(scope.row)" label="权限"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="sort" header-align="center" align="center" label="排序">
            <template slot-scope="scope">
              <el-input-number size="mini" v-model="scope.row.sort" @change="handleOrderNumChange(scope.row)" label="排序"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column prop="state" header-align="center" align="center" label="状态" width="80">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 0">禁用</span>
              <span v-else>启用</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshDataList"></add-or-update>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import util from "@/util/util";
  import AddOrUpdate from './appsysmenu-add-or-update'
  import {
    treeDataTranslate
  } from '@/utils';
  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        treelist: [],
        parenttopId: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        parentId: '',
        currentNode: ''
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
      this.getDataTree();
    },
    methods: {
      viewApi(){
        this.$http({
          url: this.$http.adornUrl('/sellerapp/appsysmenu/listApi'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({
          data
        }) => {
          if(data.code == 0){
            this.$message({
              message: "F12控制台输出查看",
              type: "success",
              duration: 1500,
            });
            console.log("API权限接口：",data)
          }else{
            this.$message.error(data.msg);
          }
        });
      },
      viewBaoBiaoApi(){
        this.$http({
          url: this.$http.adornUrl('/sellerapp/appsysmenu/baobiaoListApi'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({
          data
        }) => {
          if(data.code == 0){
            this.$message({
              message: "F12控制台输出查看",
              type: "success",
              duration: 1500,
            });
            console.log("API报表接口：hotel/appsysmenu/baobiaoListApi")
            console.log("API报表接口：",data)
            console.log("报表用户拥有列表：",data.data.resList)
            console.log("报表用户收藏列表：",data.data.collList)
          }else{
            this.$message.error(data.msg);
          }
        });
      },
      refreshDataList() {
        this.getDataList()
        this.getDataTree();
      },
      remove(node, data) {
        console.log(data)
        this.deleteHandle(data.id);
      },
      // 获取数据列表
      getDataTree() {
        this.$http({
          url: this.$http.adornUrl('/sys/menu/applist'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({
          data
        }) => {
          this.treelist = treeDataTranslate(data, 'id')
        });
      },
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sellerapp/appsysmenu/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            parentId: this.parentId
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      clickNodeTreeData(data) {
        this.parentId = data.id;
        this.type = data.type;
        this.currentNode = data;
        this.getDataList();
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      handleOrderNumChange(row) {
        this.$http({
          url: this.$http.adornUrl(`/sellerapp/appsysmenu/update`),
          method: "post",
          data: this.$http.adornData({
            id: row.id,
            name: row.name,
            reqUri: row.reqUri,
            pcName: row.pcName,
            permissions: row.permissions,
            sort: row.sort,
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataTree();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sellerapp/appsysmenu/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.refreshDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
              this.refreshDataList()
            }
          })
        })
      }
    }
  }
</script>
